<?php

/**
 * 用户模型
 *
 * @author MrHan<hcj112@163.com>
 */
class UcenterMemberModel extends Model
{
    //  自动验证
    protected $_validate = array(
        //  验证用户名
        array('username', '5,15', -1, self::EXISTS_VALIDATE, 'length'), //用户名长度不合法
        array('username', '', -2, self::EXISTS_VALIDATE, 'unique'), //用户名已被注册

        //  验证密码
        array('password', '6,30', -3, self::EXISTS_VALIDATE, 'length'), //密码长度不合法

        //  验证邮箱
        array('email', 'email', -4, self::EXISTS_VALIDATE), //  邮箱格式不正确
        array('email', '', -5, self::EXISTS_VALIDATE, 'unique'), // 邮箱已经被注册
    );

    //  自动完成
    protected $_auto = array(
        array('reg_time', 'time', 1, 'function'),
        array('password', 'md5', 3, 'function'),
        array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function', 1),
        array('update_time', NOW_TIME, 3),
        array('status', 1),
    );

    /**
     * 用户注册
     *
     * @param $username 用户名
     * @param $password 密码
     * @param $email    邮箱
     * @return int|mixed|string
     */
    public function register($username, $password, $email)
    {
        $data = array(
            'username' => $username,
            'password' => $password,
            'email' => $email,
        );

        // 用户注册
        if ($this->create($data)) {
            $uid = $this->add();
            return $uid ? $uid : 0; //0-未知错误，大于0-注册成功
        } else {
            return $this->getError(); //错误详情见自动验证注释
        }
    }

    /**
     * 用户登录
     *
     * @param $username 用户名
     * @param $password 密码
     * @param int $type 用户登录类型(1-用户名登录,2-邮箱登录)
     *
     * @return int
     */
    public function login($username, $password, $type = 1)
    {
        $map = array();
        switch ($type) {
            case 1:
                $map['username'] = $username;
                break;
            case 2:
                $map['email'] = $username;
                break;
            default:
                return 0; //未知错误
        }

        $user = $this->where($map)->find();

        if (is_array($user) && $user['status']) {
            if (md5($password) === $user['password']) {
                return $user['id'];
            } else {
                return -2; //  密码错误
            }
        } else {
            return -1; //  用户不存在或被禁用
        }
    }

    public function checkUsername($username = '')
    {
        $map['username'] = $username;
        $uid = $this->where($map)->filed('id')->find();
        if (0 < $uid) {
            return -1; //　用户名已经被占用
        } else {
            return 1; //  该用户名可以使用
        }
    }


    /**
     * 获取用户信息
     *
     * @param $uid　用户的ID或用户名
     * @param bool $is_username　是否按用户名查询
     * @return array|int
     */
    public function getUserInfo($uid, $is_username = false)
    {
        $map = array();
        if ($is_username) {
            $map['username'] = $uid;
        } else {
            $map['id'] = $uid;
        }


        $field = array('id', 'username', 'email', 'status');
        $user = $this->where($map)->field($field)->find();

        if (is_array($user) && $user['status'] = 1) {
            return array($user['id'], $user['username'], $user['email']);
        } else {
            return -1; //   用户不存在或被禁用
        }
    }


    public function updateUserInfo($data)
    {

        // 验证密码
        if (!$this->checkPasswod($data['uid'], $data['password'])) {
            $this->error = '你的原密码不正确!';
            return false;
        }

        $result = $this->create($data);

        if ($result) {
            $res = $this->save();

            return $res;
        }
        dump($this);
        //return false;
    }

    protected function checkPasswod($uid, $password)
    {
        $pwd = $this->getFieldById($uid, $field = 'password');

        if ($pwd === md5($password)) {
            return true;
        }

        return false;
    }

    public function getFieldById($uid, $field)
    {
        if (empty($uid) || empty($field)) {
            $this->error = '参数错误!';
            return false;
        }

        $data = $this->where('id=' . $uid)->field($field)->find();
        return $data[$field];
    }


}